package com.rp.article.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import com.rp.article.serivce.CommentPortalService;
import com.rp.article.serivce.impl.CommentPortalServiceImpl;
import com.rp.bo.CommentReplyBO;
import com.rp.news.common.NewsCommon;
import com.rp.news.common.PagedResult;
import com.rp.news.common.Result;
import com.rp.news.util.RedisOperator;
import com.rp.vo.AppUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@RequestMapping("comment")
@Api(value = "评论相关业务的controller", tags = {"评论相关业务的controller"})
public class CommentController {

    @Autowired
    private CommentPortalService commentPortalService;

    @Autowired
    public RedisOperator redis;


    @Autowired
    public RestTemplate restTemplate;


    @PostMapping("/createComment")
    @ApiOperation(value = "用户评论", notes = "用户评论", httpMethod = "POST")
    public Result createArticle(@RequestBody @Valid CommentReplyBO commentReplyBO) {

        //根据留言用户的id查询他的昵称,用户存入到数据表中,进行冗余,从而避免多表联合查询
        String userId = commentReplyBO.getCommentUserId();

        //发起httptemplate远程请求,通过用户服务,获取用户的昵称
        Set<String> sets = new HashSet<>();
        sets.add(userId);
        List<AppUserVo> publisherList = getPublisherList(sets);
        String nickname = publisherList.get(0).getNickname();
        String face = publisherList.get(0).getFace();

        commentPortalService.createCommnet(commentReplyBO.getArticleId(), commentReplyBO.getFatherId()
                , commentReplyBO.getContent(), userId, nickname, face);

        return Result.ok();
    }

    @GetMapping("/counts")
    @ApiOperation(value = "用户评论数查询", notes = "用户评论数查询", httpMethod = "GET")
    public Result commentCounts(String articleId) {

        Integer countsFromRedis = redis.getCountsFromRedis(CommentPortalServiceImpl.REDIS_ARTICLE_COMMENT_COUNTS + articleId);
        return Result.ok(countsFromRedis);
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询文章的所有评论列表", notes = "查询文章的所有评论列表", httpMethod = "GET")
    public Result list(String articleId, Integer page, Integer pageSize) {
        if (page == null) {
            page = NewsCommon.COMMON_START_PAGE;
        }
        if (pageSize == null) {
            pageSize = NewsCommon.COMMON_PAGE_SIZE;
        }
        PagedResult result = commentPortalService.queryArticleComments(articleId, page, pageSize);
        return Result.ok(result);
    }

    @PostMapping("/mng")
    @ApiOperation(value = "查询我的评论管理列表", notes = "查询我的评论管理列表", httpMethod = "POST")
    public Result mng(String writerId, Integer page, Integer pageSize) {

        if (page == null) {
            page = NewsCommon.COMMON_START_PAGE;
        }
        if (pageSize == null) {
            pageSize = NewsCommon.COMMON_PAGE_SIZE;

        }
        PagedResult result = commentPortalService.mng(writerId, page, pageSize);
        return Result.ok(result);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "作者删除评论", notes = "作者删除评论", httpMethod = "POST")
    public Result delete(String writerId, String commentId) {
        commentPortalService.delete(writerId, commentId);
        return Result.ok();
    }

    private List<AppUserVo> getPublisherList(Set<String> idSet) {
        String url = "http://user.rpnews.com:8003/user/queryByIds?userIds=" + JSONUtil.toJsonStr(idSet);
        ResponseEntity<Result> forEntity = restTemplate.getForEntity(url, Result.class);
        Result body = forEntity.getBody();
        List<AppUserVo> appUserVoList = new ArrayList<>();
        if (body.getStatus() == 200) {
            Object data = body.getData();
            appUserVoList = Convert.convert(new TypeReference<List<AppUserVo>>() {
            }, data);
        }
        return appUserVoList;
    }


}
